package com.demo.service.impl;

import com.demo.mapper.ApplicationMapper;
import com.demo.po.ApplicationPo;
import com.demo.service.IApplicationService;
import com.demo.service.common.BizCache;
import com.demo.service.common.BizErrorCode;
import com.demo.service.common.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.List;

@Component("applicationService")
public class ApplicationServiceImpl implements IApplicationService {

    private Logger log = LoggerFactory.getLogger(ApplicationServiceImpl.class);

    @Qualifier("applicationMapper")
    @Autowired
    private ApplicationMapper applicationMapper;

    @Override
    public int countApplications(ApplicationPo cond) {
        log.debug("countApplications starting...");
        int count = 0;
        try {
            count = applicationMapper.countApplications(cond);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("countApplications end");
        return count;
    }

    @Override
    public List<ApplicationPo> findApplicationsByPage(ApplicationPo cond) {
        log.debug("findApplicationsByPage starting...");
        List<ApplicationPo> applications = null;
        try {
            applications = applicationMapper.findApplicationsByPage(cond);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("findApplicationsByPage end");
        return applications;
    }

    @Override
    public List<ApplicationPo> findAllApplications(ApplicationPo cond) {
        log.debug("findAllApplications starting...");
        List<ApplicationPo> applications = null;
        try {
            applications = applicationMapper.findAllApplications(cond);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("findAllApplications end");
        return applications;
    }

    @Override
    public ApplicationPo getApplication(Integer applicationid) {
        log.debug("getApplication starting...");
        ApplicationPo application = null;
        try {
            application = applicationMapper.getApplication(applicationid);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("getApplication end");
        return application;
    }

    @Override
    public Integer addApplication(ApplicationPo obj) {
        log.debug("addApplication starting...");
        Integer retval = 0;
        try {
            obj.setAddtime(BizCache.getInstance().getNow());
            retval = applicationMapper.addApplication(obj);

            if (retval == 0) {
                throw new BizException(BizErrorCode.EX_SQL_FAIL);
            }
            retval = obj.getApplicationid();
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("addApplication end");
        return retval;
    }

    @Override
    public int updateApplication(ApplicationPo obj) {
        log.debug("updateApplication starting...");
        int retval = 0;
        try {
            obj.setUpdatetime(BizCache.getInstance().getNow());
            retval = applicationMapper.updateApplication(obj);
            if (retval == 0) {
                throw new BizException(BizErrorCode.EX_SQL_FAIL);
            }
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("updateApplication end");
        return retval;
    }

    @Override
    public int deleteApplication(Integer applicationid) {
        log.debug("deleteApplication starting...");
        int retval = 0;
        try {
            retval = applicationMapper.deleteApplication(applicationid);
        } catch (SQLException ex) {
            log.error("exception:", ex);
            throw new BizException(BizErrorCode.EX_TRANSACTION_FAIL);
        }
        log.debug("deleteApplication end");
        return retval;
    }
}